﻿using System;
using System.Windows.Forms;
using Client.Controllers;
using Client.Interfaces;
using System.Threading;

namespace Client.UserControls
{
    internal partial class Login : UserControl, ILoginView
    {
        private ILoginController controller;
        public string Message { get { return message.Text; } set { message.Text = value; } }

        internal Login(IMainController main)
        {
            InitializeComponent();

            controller = new LoginController(main, this);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            btnLogin.Enabled = false;
            btnLogin.Text = "Please wait...";
            Message = string.Empty;
            if (tbUsername.Text.Length == 0)
            {
                Message = "Please enter a username";
                return;
            }
            if (tbPassword.Text.Length == 0)
            {
                Message = "Please enter a password";
                return;
            }
            if (!controller.LoginUser(tbUsername.Text, tbPassword.Text))
            {
                tbPassword.Text = string.Empty;
            }
            btnLogin.Text = "Login";
            btnLogin.Enabled = true;
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //Thread.Sleep(10);
            tbUsername.Focus();
        }
    }
}
